import { defineStore } from "pinia";

import { localStorageUtils, LOCAL_USER, LOCAL_MENU, LOCAL_EDIFICE_INFO } from "@gonsin/gview";
import { getEdificeApi, getMenusApi, userLoginApi } from "@/api/user";
import { useWebsiteStore } from "@gonsin/gview";
import { useSettingStore } from "~/stores";
import type { SideMenu } from "~/types/menu";

interface UserInfo {
	admin: boolean;
	audit: boolean;
	createTime: string;
	departmentKey: string;
	edificeKey: string;
	email: string;
	freeAudit: boolean;
	groupKey: string;
	groupName: string;
	imageUrl: string;
	language: string;
	permissions: string[];
	phone: string;
	realName: string;
	root: boolean;
	rootUrl: string;
	rootUserKey: string;
	updateTime: string;
	userKey: string;
	userMessageKey: string;
	username: string;
	version: number;
	token: string;
}
interface EdificeInfo {
	edificeKey: string;
	name: string;
	address: string;
	host: string;
	logoUrl: string;
	adminUserKey: string;
}

interface MenuItem {
	menuKey: string;
	edificeKey: string;
	name: string;
	sort: number;
	title: string;
	parent: string | null;
	url: string;
	icon: string | null;
}

export const useUserStore = defineStore("user", {
	state: (): {
		user: UserInfo;
		menu: MenuItem[];
		edificeInfo: EdificeInfo;
	} => ({
		user: localStorageUtils.getItem(LOCAL_USER) ?? {},
		menu: localStorageUtils.getItem(LOCAL_MENU) ?? {},
		edificeInfo: localStorageUtils.getItem(LOCAL_EDIFICE_INFO) ?? {},
	}),
	getters: {
		permissions: (state) => {
			return state.user.permissions;
		},
		token: (state) => {
			return state.user.token;
		},
		adminUserKey: (state) => {
			return state.edificeInfo.adminUserKey ?? "";
		},
		isAdminUser: (state) => {
			const adminUserKey = state.edificeInfo.adminUserKey ?? "";
			return state.user.userKey === adminUserKey;
		},
	},
	actions: {
		/**
		 * @description: 获取会议中心信息
		 * @param {string} edificeKey
		 */
		async getEdificeInfo(edificeKey: string) {
			const websiteStore = useWebsiteStore();
			const res = await getEdificeApi(edificeKey);

			if (res.state == 200) {
				this.edificeInfo = res.data;
				localStorageUtils.setItem(LOCAL_EDIFICE_INFO, this.edificeInfo);
				if (res.data.logoUrl ?? "" !== "") {
					// console.log(res.data.logoUrl);
					websiteStore.setEdificeLogoUrl(res.data.logoUrl);
				}
			}
		},
		/**
		 * @description: 获取用户菜单原始信息
		 */
		async getUserMenus() {
			const res = await getMenusApi();

			if (res.state == 200) {
				const menuData = res.data;
				this.menu = menuData;
				localStorageUtils.setItem(LOCAL_MENU, menuData);
			} else {
				throw res;
			}
		},

		async userLogin(loginParams: { username: string; password: string }) {
			const settingStore = useSettingStore();
			const res = await userLoginApi(loginParams);

			if (res.state == 200) {
				this.user = res.data;
				localStorageUtils.setItem(LOCAL_USER, res.data);

				const { language } = this.user;
				await settingStore.setUserLang(language);

				await settingStore.initUserToken(res.data.token);
				await this.getUserMenus();

				if (this.user && this.user.edificeKey) {
					// await this.getEdificeInfo(this.user.edificeKey);
				}
			} else {
				throw res;
			}
		},
	},
});
